package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.CheckGroupDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;

    @Override
    public List<CheckGroup> findAll() {
        List<CheckGroup> checkGroupList = checkGroupDao.findAll();
        return checkGroupList;
    }

    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<CheckGroup> groupPage = checkGroupDao.selectByCondition(queryString);
        return new PageResult(groupPage.getTotal(),groupPage.getResult());
    }

    @Override
    @Transactional
    public void add(CheckGroup checkGroup, Integer[] checkItemIds) {
        checkGroupDao.add(checkGroup);
        for (Integer checkItemId:checkItemIds
             ) {
            Map map = new HashMap<>();
            map.put("checkgroup_id",checkGroup.getId());
            map.put("checkitem_id",checkItemId);
            checkGroupDao.addCheckGroupAndCheckItem(map);
        }

    }

    @Override
    public CheckGroup getById(Integer id) {
        CheckGroup checkGroup = checkGroupDao.getById(id);
        return checkGroup;
    }


}
